
package com.joolun.cloud.pay.api.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.binarywang.wxpay.bean.applyment.ApplymentStateQueryResult;
import com.github.binarywang.wxpay.bean.applyment.WxPayApplyment4SubCreateRequest;
import com.github.binarywang.wxpay.bean.applyment.WxPayApplymentCreateResult;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.Applyment4SubService;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.Applyment4SubServiceImpl;
import com.joolun.cloud.pay.api.config.WxPayConfiguration;
import com.joolun.cloud.pay.common.entity.PayApplyForm;
import com.joolun.cloud.pay.api.mapper.PayApplyFormMapper;
import com.joolun.cloud.pay.api.service.PayApplyFormService;
import com.joolun.cloud.pay.common.feign.FeignShopInfoService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

/**
 * 支付进件申请单
 *
 * @author www.joolun.com
 * @date 2020-08-27 10:51:37
 */
@Service
@AllArgsConstructor
public class PayApplyFormServiceImpl extends ServiceImpl<PayApplyFormMapper, PayApplyForm> implements PayApplyFormService {

	private final FeignShopInfoService feignShopInfoService;

	@Override
	public WxPayApplymentCreateResult submit(PayApplyForm payApplyForm) throws WxPayException {
		WxPayApplyment4SubCreateRequest request = new WxPayApplyment4SubCreateRequest();
		payApplyForm = baseMapper.selectById(payApplyForm.getId());
		request.setBusinessCode(payApplyForm.getId());
		WxPayApplyment4SubCreateRequest.ContactInfo contactInfo = JSONUtil.toBean(payApplyForm.getContactInfo(),WxPayApplyment4SubCreateRequest.ContactInfo.class);
		WxPayApplyment4SubCreateRequest.SubjectInfo subjectInfo = JSONUtil.toBean(payApplyForm.getSubjectInfo(),WxPayApplyment4SubCreateRequest.SubjectInfo.class);
		WxPayApplyment4SubCreateRequest.BusinessInfo businessInfo = JSONUtil.toBean(payApplyForm.getBusinessInfo(),WxPayApplyment4SubCreateRequest.BusinessInfo.class);
		WxPayApplyment4SubCreateRequest.SettlementInfo settlementInfo = JSONUtil.toBean(payApplyForm.getSettlementInfo(),WxPayApplyment4SubCreateRequest.SettlementInfo.class);
		WxPayApplyment4SubCreateRequest.BankAccountInfo bankAccountInfo = JSONUtil.toBean(payApplyForm.getBankAccountInfo(),WxPayApplyment4SubCreateRequest.BankAccountInfo.class);
		request.setContactInfo(contactInfo);
		request.setSubjectInfo(subjectInfo);
		request.setBusinessInfo(businessInfo);
		request.setSettlementInfo(settlementInfo);
		request.setBankAccountInfo(bankAccountInfo);
		WxPayConfig wxPayConfig = new WxPayConfig();
		WxPayService wxPayService = WxPayConfiguration.getPayService(wxPayConfig);
		Applyment4SubService applyment4SubService = new Applyment4SubServiceImpl(wxPayService);
		WxPayApplymentCreateResult wxPayApplymentCreateResult = applyment4SubService.createApply(request);
		String applymentId = wxPayApplymentCreateResult.getApplymentId();
		payApplyForm.setApplymentId(applymentId);
		//更新ApplymentId
		baseMapper.updateById(payApplyForm);
		return wxPayApplymentCreateResult;
	}

	@SneakyThrows
	@Override
	public void queryApplyStatus(PayApplyForm payApplyForm) {
		payApplyForm = baseMapper.selectById(payApplyForm.getId());
		WxPayConfig wxPayConfig = new WxPayConfig();
		WxPayService wxPayService = WxPayConfiguration.getPayService(wxPayConfig);
		Applyment4SubService applyment4SubService = new Applyment4SubServiceImpl(wxPayService);
		//查询查询申请状态
		ApplymentStateQueryResult applymentStateQueryResult = applyment4SubService.queryApplyStatusByApplymentId(payApplyForm.getApplymentId());
		//更新
		payApplyForm.setApplymentId(payApplyForm.getApplymentId());
		payApplyForm.setSubMchid(applymentStateQueryResult.getSubMchid());
		payApplyForm.setApplymentState(applymentStateQueryResult.getApplymentState().toString());
		payApplyForm.setApplymentStateMsg(applymentStateQueryResult.getApplymentStateMsg());
		payApplyForm.setSignUrl(applymentStateQueryResult.getSignUrl());
		payApplyForm.setAuditDetail(applymentStateQueryResult.getAuditDetail().size()>0 ? JSONUtil.parseObj(applymentStateQueryResult.getAuditDetail().get(0)) : null);
		baseMapper.updateById(payApplyForm);
	}
}
